
import com.mit.web.business.proxy.DesignProxy;
import com.mit.web.business.remote.IInvokeResponder;
import com.mit.web.config.Constants;
import com.mit.web.event.PopWinCloseEvent;
import com.mit.web.model.design.StyleEnquiryModel;
import com.mit.web.util.LogUtil;
import com.mit.web.util.MessageUtil;
import com.mit.web.view.component.monitor.IProgressMonitor;
import com.mit.web.view.design.StyleDetailWindow;

import flash.events.MouseEvent;

import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.controls.PopUpMenuButton;
import mx.events.FlexEvent;
import mx.events.ListEvent;
import mx.events.MenuEvent;
import mx.logging.ILogger;
import mx.managers.PopUpManager;
import mx.resources.ResourceManager;
import mx.rpc.events.ResultEvent;

[Bindable] private var _model:StyleEnquiryModel;
private var _pm:IProgressMonitor;
private var _proxy:DesignProxy = DesignProxy.getInstance();
private var _logger:ILogger;

/**
 * 初始化事件绑定在datagrid上，避免查询时间长，整版页面没load出来
 * */
private function init(event:FlexEvent):void{
	_logger = LogUtil.getLogger(this);
	_logger.debug("DataGrid created, load all records");
	
	_model = new StyleEnquiryModel();
	doEnquiry();
}

/**
 * 输入搜索条件后按回车键
 * */
private function doSearch():void{
	//跳回第一页
	_model.pageNo = 1;
	_model.searchCode =txt_search.text;	
	doEnquiry(_model.searchCode);
}

/**
 * 点击刷新按钮
 * */
private function doRefresh(event:MouseEvent):void{
	_model.searchCode = null;
	_model.searchState = null;
	_model.pageNo = 1;
	doEnquiry();
}

/**
 * 选择状态
 * */
private function doFilter(event:MenuEvent):void{
	_model.pageNo = 1;
	_model.searchState = event.item.data;
	_model.searchCode =txt_search.text;	
	//更改Popupmenubutton的标签
	(event.currentTarget as PopUpMenuButton).label = event.label;
	doEnquiry(_model.searchCode, _model.searchState,  _model.pageNo);
}

/**
 * 从数据库中读取分页后的数据
 * */
private function doEnquiry(code:String = null, state:String = null, pageNo:int = 1):void{
	//读取记录集
	var responder:IInvokeResponder = _proxy.getPagedStyles(pageNo, code, state);
	responder.addResultListener(retrieveListHandler);
	responder.addFaultListener(exceptionHandler);
	
	var r2:IInvokeResponder = _proxy.getStylesCount(code, state);
	r2.addResultListener(retrieveRCHandler);
	r2.addFaultListener(exceptionHandler);
}

private function retrieveListHandler(event:ResultEvent):void{
	var lst:ArrayCollection = event.result as ArrayCollection;
	//有记录返回才更新，否则只是提示无记录存在
	if(lst != null && lst.length > 0){
		_model.styleLst = lst;
		dg.validateNow();
	}
	else
		MessageUtil.popNoRecordMessage(this);
}

private function retrieveRCHandler(event:ResultEvent):void{
	var rc:int = event.result as int;
	//与取回记录的行为保持一致，如果没有记录找到，不做任何动作。
	if(rc > 0){
		_logger.debug("Total record: " + rc.toString());
		_model.recordCount = rc;
	}
}

/**
 * 弹出新增窗口
 * */
protected function bnt_new_clickHandler(event:MouseEvent):void
{
	var window:StyleDetailWindow = new StyleDetailWindow();	 
	window.addEventListener(PopWinCloseEvent.CREATED, createRecordHandler);
	PopUpManager.addPopUp(window,this,true);
	PopUpManager.centerPopUp(window);
}

/**
 * 双击一条记录以编辑
 * */
private function doEdit(event:ListEvent):void{
	var item:Object = dg.selectedItem;
	if(item){
		var id:int = item.id;
		var window:StyleDetailWindow = new StyleDetailWindow();	 
		window.addEventListener(PopWinCloseEvent.DELETED, editRecordHandler);
		window.addEventListener(PopWinCloseEvent.SAVED, editRecordHandler);
		window.addEventListener(PopWinCloseEvent.APPROVED, editRecordHandler);
		window.addEventListener(PopWinCloseEvent.TERMINATED, editRecordHandler);
		window.title = item.code;
		window.styleId = id;
		PopUpManager.addPopUp(window,this,true);
		PopUpManager.centerPopUp(window)
	}
}
private function createRecordHandler(event:PopWinCloseEvent):void{
	showMessageTip(event.msg, dg);
	//新建后一定显示第一页。
	doEnquiry();
}

private function editRecordHandler(event:PopWinCloseEvent):void{
	showMessageTip(event.msg, dg);
	//*****************************************
	//****************TODO: 此处选择了重新查询来刷新列表，如果性能有问题，尝试使用不刷新更新列表！
	//****************event.vo = returned vo;
	//*****************************************
	doEnquiry(_model.searchCode, _model.searchState, _model.pageNo);
	//TODO 如何能让datagrid默认选中编辑的那条记录呢？
}

//Call back function for pagination
//Caution: the parameter is fixed! otherwise, the pagination may not work
private function goToPage(pageNo:int):void{
	_model.pageNo = pageNo;
	doEnquiry(_model.searchCode, _model.searchState, _model.pageNo);
}


protected function btn_state_creationCompleteHandler(event:FlexEvent):void
{
	var source:ArrayCollection = new ArrayCollection();
	var all:String = ResourceManager.getInstance().getString(Constants.BUNDLE_COMMON,"label.selectAll");
	source.addItem({label:all});
	source.addItem({type:"separator"});
	source.addAll(Constants.STYLE_STATE);
	(event.currentTarget as PopUpMenuButton).dataProvider = source;
}


